/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package origendatos.dyd.model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import origendatos.dyd.model.exceptions.NonexistentEntityException;
import origendatos.dyd.model.exceptions.PreexistingEntityException;

/**
 *
 * @author erodrig
 */
public class ArmasJpaController {

    public ArmasJpaController() {
        emf = Persistence.createEntityManagerFactory("OrigenDatosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Armas armas) throws PreexistingEntityException, Exception {
        if (armas.getPjList() == null) {
            armas.setPjList(new ArrayList<Pj>());
        }
        if (armas.getPjList1() == null) {
            armas.setPjList1(new ArrayList<Pj>());
        }
        if (armas.getPjList2() == null) {
            armas.setPjList2(new ArrayList<Pj>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Pj> attachedPjList = new ArrayList<Pj>();
            for (Pj pjListPjToAttach : armas.getPjList()) {
                pjListPjToAttach = em.getReference(pjListPjToAttach.getClass(), pjListPjToAttach.getId());
                attachedPjList.add(pjListPjToAttach);
            }
            armas.setPjList(attachedPjList);
            List<Pj> attachedPjList1 = new ArrayList<Pj>();
            for (Pj pjList1PjToAttach : armas.getPjList1()) {
                pjList1PjToAttach = em.getReference(pjList1PjToAttach.getClass(), pjList1PjToAttach.getId());
                attachedPjList1.add(pjList1PjToAttach);
            }
            armas.setPjList1(attachedPjList1);
            List<Pj> attachedPjList2 = new ArrayList<Pj>();
            for (Pj pjList2PjToAttach : armas.getPjList2()) {
                pjList2PjToAttach = em.getReference(pjList2PjToAttach.getClass(), pjList2PjToAttach.getId());
                attachedPjList2.add(pjList2PjToAttach);
            }
            armas.setPjList2(attachedPjList2);
            em.persist(armas);
            for (Pj pjListPj : armas.getPjList()) {
                Armas oldArmasOfPjListPj = pjListPj.getArmas();
                pjListPj.setArmas(armas);
                pjListPj = em.merge(pjListPj);
                if (oldArmasOfPjListPj != null) {
                    oldArmasOfPjListPj.getPjList().remove(pjListPj);
                    oldArmasOfPjListPj = em.merge(oldArmasOfPjListPj);
                }
            }
            for (Pj pjList1Pj : armas.getPjList1()) {
                Armas oldArmas1OfPjList1Pj = pjList1Pj.getArmas1();
                pjList1Pj.setArmas1(armas);
                pjList1Pj = em.merge(pjList1Pj);
                if (oldArmas1OfPjList1Pj != null) {
                    oldArmas1OfPjList1Pj.getPjList1().remove(pjList1Pj);
                    oldArmas1OfPjList1Pj = em.merge(oldArmas1OfPjList1Pj);
                }
            }
            for (Pj pjList2Pj : armas.getPjList2()) {
                Armas oldArmas2OfPjList2Pj = pjList2Pj.getArmas2();
                pjList2Pj.setArmas2(armas);
                pjList2Pj = em.merge(pjList2Pj);
                if (oldArmas2OfPjList2Pj != null) {
                    oldArmas2OfPjList2Pj.getPjList2().remove(pjList2Pj);
                    oldArmas2OfPjList2Pj = em.merge(oldArmas2OfPjList2Pj);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findArmas(armas.getId()) != null) {
                throw new PreexistingEntityException("Armas " + armas + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Armas armas) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Armas persistentArmas = em.find(Armas.class, armas.getId());
            List<Pj> pjListOld = persistentArmas.getPjList();
            List<Pj> pjListNew = armas.getPjList();
            List<Pj> pjList1Old = persistentArmas.getPjList1();
            List<Pj> pjList1New = armas.getPjList1();
            List<Pj> pjList2Old = persistentArmas.getPjList2();
            List<Pj> pjList2New = armas.getPjList2();
            List<Pj> attachedPjListNew = new ArrayList<Pj>();
            for (Pj pjListNewPjToAttach : pjListNew) {
                pjListNewPjToAttach = em.getReference(pjListNewPjToAttach.getClass(), pjListNewPjToAttach.getId());
                attachedPjListNew.add(pjListNewPjToAttach);
            }
            pjListNew = attachedPjListNew;
            armas.setPjList(pjListNew);
            List<Pj> attachedPjList1New = new ArrayList<Pj>();
            for (Pj pjList1NewPjToAttach : pjList1New) {
                pjList1NewPjToAttach = em.getReference(pjList1NewPjToAttach.getClass(), pjList1NewPjToAttach.getId());
                attachedPjList1New.add(pjList1NewPjToAttach);
            }
            pjList1New = attachedPjList1New;
            armas.setPjList1(pjList1New);
            List<Pj> attachedPjList2New = new ArrayList<Pj>();
            for (Pj pjList2NewPjToAttach : pjList2New) {
                pjList2NewPjToAttach = em.getReference(pjList2NewPjToAttach.getClass(), pjList2NewPjToAttach.getId());
                attachedPjList2New.add(pjList2NewPjToAttach);
            }
            pjList2New = attachedPjList2New;
            armas.setPjList2(pjList2New);
            armas = em.merge(armas);
            for (Pj pjListOldPj : pjListOld) {
                if (!pjListNew.contains(pjListOldPj)) {
                    pjListOldPj.setArmas(null);
                    pjListOldPj = em.merge(pjListOldPj);
                }
            }
            for (Pj pjListNewPj : pjListNew) {
                if (!pjListOld.contains(pjListNewPj)) {
                    Armas oldArmasOfPjListNewPj = pjListNewPj.getArmas();
                    pjListNewPj.setArmas(armas);
                    pjListNewPj = em.merge(pjListNewPj);
                    if (oldArmasOfPjListNewPj != null && !oldArmasOfPjListNewPj.equals(armas)) {
                        oldArmasOfPjListNewPj.getPjList().remove(pjListNewPj);
                        oldArmasOfPjListNewPj = em.merge(oldArmasOfPjListNewPj);
                    }
                }
            }
            for (Pj pjList1OldPj : pjList1Old) {
                if (!pjList1New.contains(pjList1OldPj)) {
                    pjList1OldPj.setArmas1(null);
                    pjList1OldPj = em.merge(pjList1OldPj);
                }
            }
            for (Pj pjList1NewPj : pjList1New) {
                if (!pjList1Old.contains(pjList1NewPj)) {
                    Armas oldArmas1OfPjList1NewPj = pjList1NewPj.getArmas1();
                    pjList1NewPj.setArmas1(armas);
                    pjList1NewPj = em.merge(pjList1NewPj);
                    if (oldArmas1OfPjList1NewPj != null && !oldArmas1OfPjList1NewPj.equals(armas)) {
                        oldArmas1OfPjList1NewPj.getPjList1().remove(pjList1NewPj);
                        oldArmas1OfPjList1NewPj = em.merge(oldArmas1OfPjList1NewPj);
                    }
                }
            }
            for (Pj pjList2OldPj : pjList2Old) {
                if (!pjList2New.contains(pjList2OldPj)) {
                    pjList2OldPj.setArmas2(null);
                    pjList2OldPj = em.merge(pjList2OldPj);
                }
            }
            for (Pj pjList2NewPj : pjList2New) {
                if (!pjList2Old.contains(pjList2NewPj)) {
                    Armas oldArmas2OfPjList2NewPj = pjList2NewPj.getArmas2();
                    pjList2NewPj.setArmas2(armas);
                    pjList2NewPj = em.merge(pjList2NewPj);
                    if (oldArmas2OfPjList2NewPj != null && !oldArmas2OfPjList2NewPj.equals(armas)) {
                        oldArmas2OfPjList2NewPj.getPjList2().remove(pjList2NewPj);
                        oldArmas2OfPjList2NewPj = em.merge(oldArmas2OfPjList2NewPj);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = armas.getId();
                if (findArmas(id) == null) {
                    throw new NonexistentEntityException("The armas with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Armas armas;
            try {
                armas = em.getReference(Armas.class, id);
                armas.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The armas with id " + id + " no longer exists.", enfe);
            }
            List<Pj> pjList = armas.getPjList();
            for (Pj pjListPj : pjList) {
                pjListPj.setArmas(null);
                pjListPj = em.merge(pjListPj);
            }
            List<Pj> pjList1 = armas.getPjList1();
            for (Pj pjList1Pj : pjList1) {
                pjList1Pj.setArmas1(null);
                pjList1Pj = em.merge(pjList1Pj);
            }
            List<Pj> pjList2 = armas.getPjList2();
            for (Pj pjList2Pj : pjList2) {
                pjList2Pj.setArmas2(null);
                pjList2Pj = em.merge(pjList2Pj);
            }
            em.remove(armas);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Armas> findArmasEntities() {
        return findArmasEntities(true, -1, -1);
    }

    public List<Armas> findArmasEntities(int maxResults, int firstResult) {
        return findArmasEntities(false, maxResults, firstResult);
    }

    private List<Armas> findArmasEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Armas.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Armas findArmas(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Armas.class, id);
        } finally {
            em.close();
        }
    }

    public int getArmasCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Armas> rt = cq.from(Armas.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
